package com.zwk.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwk.base.entity.CommonResult;
import com.zwk.base.handler.GuLiException;
import com.zwk.base.util.JwtUtils;
import com.zwk.edu.client.MemberClient;
import com.zwk.edu.entity.Comment;
import com.zwk.edu.entity.Course;
import com.zwk.edu.mapper.CommentMapper;
import com.zwk.edu.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author zwk
 * @since 2022-08-31
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private MemberClient memberClient;

    @Autowired
    private CommentMapper commentMapper;

    // 接受前端传来的评论数据, 通过token获取用户数据
    @Override
    public Map<String, Object> saveComment(Comment comment, HttpServletRequest request) {
        if (comment != null){
            if (ObjectUtils.isEmpty(comment.getCourseId())){
                throw new GuLiException(20001, "课程id为空");
            }
            if (ObjectUtils.isEmpty(comment.getTeacherId())){
                throw new GuLiException(20001, "讲师id为空");
            }
            if (ObjectUtils.isEmpty(comment.getContent())){
                throw new GuLiException(20001, "评论内容为空");
            }

            CommonResult memberInfo = getUserInfoByToken(request);
            System.out.println(memberInfo.toString());
            Map<String, Object> data = memberInfo.getData();
            comment.setMemberId((String) data.get("memberId"));
            comment.setNickname((String) data.get("nickname"));
            comment.setAvatar((String) data.get("avatar"));

            // 添加数据
            int result = commentMapper.insert(comment);
            if (result == 1){
                return data;
            }
            throw new GuLiException(20001, "评论失败");
        }
        throw new GuLiException(20001, "评论数据为空");
    }

    // 根据课程id查询评论信息
    @Override
    public Map<String, Object> getCommentPageList(String id, Page<Comment> page) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", id);
        wrapper.orderByDesc("gmt_create");
        commentMapper.selectPage(page, wrapper);
        if (page.getCurrent() > page.getPages()){
            page.setCurrent(page.getPages());
            commentMapper.selectPage(page, wrapper);
        }

        // 封装数据
        Map<String, Object> map = new HashMap<>();
        List<Comment> records = page.getRecords();
        long current = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext(); // 上一页
        boolean hasPrevious = page.hasPrevious(); // 下一页
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    /**
     * 通过token获取用户信息
     */
    private CommonResult getUserInfoByToken(HttpServletRequest request){
        String id = JwtUtils.getMemberIdByJwtToken(request);
        if (ObjectUtils.isEmpty(id)){
            throw new GuLiException(20001, "您还没有登录");
        }
        return memberClient.getMemberInfoById(id);
    }
}
